import unittest
import json
import os
import sys
import logging
import warnings
import threading
import time
import pandas as pd

path = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
sys.path.insert(0, path)
from contract_market.main import main, test_main # NOQA
from contract_market.base.event import Event , EventData # NOQA
from contract_market.common.constant import *  # NOQA
from contract_market.utils.restruct import run_subscribe
from contract_market.server.server import MainProcess


def do_request(engine):
    event_data = EventData()
    e = Event(XT_ACCOUNT_INFO_EVENT, event_data)
    engine.send(e)
    print(e.event_data.result)


class TestFocus(unittest.TestCase):

    @unittest.skip
    def test_init(self):

        engine = test_main()
        topic = "1inch_usdt"
        queue = engine.on_ticker(topic)
        logger = logging.getLogger("simpleExample")

        while 1:
            # time.sleep(0.1)
            try:
                res = queue.get()
                print("[ticker]  ", res)
                logger.debug(res)
            except Exception as e:
                print("waiting", e)
                pass

    @unittest.skip
    def test_get_account_info(self):

        engine = test_main()
        tasks = []
        for _ in range(100):
            t = threading.Thread(target=do_request, args=(engine, ))
            tasks.append(t)

        for t in tasks:
            t.start()
        
        for t in tasks:
            t.join()

    @unittest.skip
    def test_listen_user_order(self):
        """ """
        engine = test_main()
        topic = "b1000aa0-9119-4ce7-a963-e7a0fe0055cb"
        queue = engine.on_user_trade(topic)

        while 1:
            time.sleep(0.1)
            try:
                res = queue.get()
                print("[test_listen_user_order] res:: ", res, type(res))
            except Exception as e:
                print("waiting", e)
                pass

    @unittest.skip
    def test_listen_user_position(self):
        """ """
        mp = MainProcess()
        engine = mp.init_engine()
        # engine = test_main()
        topic = "b1000aa0-9119-4ce7-a963-e7a0fe0055cb"
        queue = engine.on_user_position(topic)

        while 1:
            # time.sleep(0.1)
            try:
                res = queue.get()
                print("[test_listen_user_order] res:: ", res)
            except Exception as e:
                print("waiting", e)
                pass
    
    @unittest.skip
    def test_listen_index_price(self):
        # engine = test_main()
        mp = MainProcess()
        engine = mp.init_engine()
        topic = "dot_usdt"
        queue = engine.on_index_price(topic)

        while 1:
            time.sleep(0.1)
            try:
                res = queue.get()
                print("[test_listen_user_order] res:: ", res)
            except Exception as e:
                print("waiting", e)
                pass
    
    # @unittest.skip
    def test_restruct(self):
        """ """
        # engine = test_main()
        # mp = MainProcess()
        # engine = mp.init_engine()
        # topic = "b1000aa0-9119-4ce7-a963-e7a0fe0055cb"
        # queue = engine.on_user_trade(topic)
        # restruct = Restruct()
        # while 1:
        #     try:
        #         res = queue.get()
        #         restruct.orderbook('dot_usdt', topic, res)
        #     except Exception as e:
        #         print("waiting", e)
        #         pass
        
        # run_subscribe()
        test_main()

    
if __name__ == "__main__":
    unittest.main()
